import numpy as np
def calculate_metrics(real_scores, fake_scores, threshold):
    """
    计算给定阈值下的各项指标
    """
    real_scores = np.array(real_scores)
    fake_scores = np.array(fake_scores)

    # 计算基础指标
    TP = np.sum(real_scores >= threshold)  # 真脸被正确识别为真脸
    FN = np.sum(real_scores < threshold)  # 真脸被错误识别为假脸
    FP = np.sum(fake_scores >= threshold)  # 假脸被错误识别为真脸
    TN = np.sum(fake_scores < threshold)  # 假脸被正确识别为假脸

    # 计算评估指标
    recall = TP / (TP + FN) if (TP + FN) > 0 else 0  # 真脸被正确识别的比例
    frr = FN / (TP + FN) if (TP + FN) > 0 else 0  # 真脸被错误拒绝的比例
    far = FP / (FP + TN) if (FP + TN) > 0 else 0  # 假脸被错误接受的比例
    hter = (far + frr) / 2  # 半总错误率

    return {
        'TP': int(TP),
        'TN': int(TN),
        'FP': int(FP),
        'FN': int(FN),
        'recall': recall,
        'frr': frr,
        'far': far,
        'hter': hter
    }